package com.schpro.webexperiment.config;

import com.schpro.webexperiment.model.Result;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常捕获
 *
 * @author lss
 * @date 2019-02-26, Tue
 */
@ControllerAdvice(basePackages = {"com.schpro.webexeperiment.control"})
@Slf4j
public class GlobalExceptionHandler {

  /**
   * error page message
   */
  private static final String DEFAULT_ERROR_MESSAGE = "Sorry，something went error . . .";

  @ExceptionHandler(Exception.class)
  @ResponseBody
  public Result errorHandle(HttpServletRequest request, Exception e) {
    String url = request.getRequestURL().toString();
    log.error("Access to {} occur error -> \n{}", url, e);
    Result<String> r = new Result<>();
    r.setCode(500);
    r.setMessage("Access to 【 -> " + url + " 】occur error -> data for detail" + e.getMessage());
    r.setData(e.getMessage());
    return r;
  }
}